import time
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed

# 目标URL
url = "https://www.kongfz.com"


# 定义一个函数，用于请求URL并记录响应时间
def make_request(thread_id):
    print(f"线程 {thread_id} 开始请求")
    start_time = time.time()
    response = requests.get(url)
    end_time = time.time()
    response_time = end_time - start_time
    print(f"线程 {thread_id} 完成请求，响应时间: {response_time:.2f}秒")
    return response_time


# 并发线程数
num_threads = 100

# 记录响应时间的列表
response_times = []

# 使用ThreadPoolExecutor进行并发请求
with ThreadPoolExecutor(max_workers=num_threads) as executor:
    futures = [executor.submit(make_request, i) for i in range(num_threads)]

    for future in as_completed(futures):
        response_time = future.result()
        response_times.append(response_time)

# 计算并打印性能测试结果
average_response_time = sum(response_times) / len(response_times)
max_response_time = max(response_times)
min_response_time = min(response_times)

print(f"平均响应时间: {average_response_time:.2f}秒")
print(f"最大响应时间: {max_response_time:.2f}秒")
print(f"最小响应时间: {min_response_time:.2f}秒")
